/* 拓扑排序
* 1.做法:
    (1) 合并两个集合
    (2) 查询某个元素的祖宗节点
    (3) 记录方法:
        记录每个集合大小: 绑定到根节点上
        记录每个点到根节点的距离: 绑定到每个元素上

* 2.离散化:
    (1) 保留排序: 排序，判重，二分
    (2) 不保留排序: map, hash

* 本题: 
    变量范围较大, 使用离散化将x的范围映射
    离散化, 相等条件合并, 依次判断每个不等条件
    
*/

#pragma GCC optimize("O1,O2,O3,Ofast")
#pragma GCC optimize("no-stack-protector,unroll-loops,fast-math,inline")
#pragma GCC target("avx,avx2,fma")
#pragma GCC target("sse,sse2,sse3,sse4,sse4.1,sse4.2,ssse3")

#include <iostream>
#include <cstring>
#include <algorithm> 
#include <vector>
#include <unordered_map>
// #define ONLINE_GUDGE
using namespace std;
using PII = pair<int, int>;
#define wei first
#define val second
const int N = 1e6+10;

int n, cnt, fa[N*2];
vector<PII> eq, neq;
unordered_map<int, int> hsh;

inline int find(int x)
{
    if(x == fa[x]) return x;
    else return fa[x] = find(fa[x]);
}

inline int map(int x) {
    if (hsh.count(x)) {
        return hsh[x];
    }
    return hsh[x] = cnt++;
}

inline void merge(int x) // x <= y
{
    int fx = find(x);
    if(x == fx) return;
    fa[x] = fx;
}

inline void Print()
{
    cout << "fa[]:\n";
    for(int i = 1; i <= n; i++){
        printf("fa[%d]:%d\n", i, fa[i]);
    }
}

int main()
{

    #ifdef ONLINE_JUDGE
    ios::sync_with_stdio(false);   
	cin.tie(0);
    #else
    freopen("./in.txt","r",stdin);
    #endif
    int T; cin >> T;
    while(T--){
        cin >> n; cnt = 0;
        hsh.clear(); eq.clear(); neq.clear();

        for(int i = 1; i <= n; i++){
            int x, y, op; cin >> x >> y >> op;
            x = map(x), y = map(y);
            if(op) eq.push_back({x, y});
            else neq.push_back({x, y}); 
        } 
        for (int i = 0; i < cnt; i++) fa[i] = i;
        
        for (auto item : eq) {
            fa[find(item.first)] = find(item.second);
        }
        bool flag = true;
        for (auto item : neq) {
            if (find(item.first) == find(item.second)) {
                flag = false;
                break;
            }
        }
        cout << (flag ? "YES" : "NO") << endl;

    }
    
    return 0;
}
